package com.uni.controller;

import com.uni.pojo.User;
import com.uni.service.UserService;
import com.uni.vo.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public String login(User user, HttpSession session){
        User loginUser = userService.login(user);
        session.setAttribute("loginUser", loginUser);
        return loginUser == null ? RestResponse.fail(loginUser)
                : RestResponse.success(loginUser);
    }
    @GetMapping("/check")
    public String check(){
        return RestResponse.success("响应成功!");
    }
    @PostMapping("/register")
    public String register(User user, HttpSession session){
        if(user == null)
            return RestResponse.fail("注册信息为空");
        int i = userService.register(user);
        return i > 0 ? RestResponse.success("注册成功!") : RestResponse.fail("注册失败");
    }
}
